package com.qidian.server.workerThread;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

import com.qidian.common.Constants;
import com.qidian.common.Globals;
import com.qidian.common.beans.ClientBean;
import com.qidian.common.beans.RechargeCardBean;
import com.qidian.connection.util.ConnectionPool;

public final class Synchronizer_Client extends WorkerThread
{
	private static Logger logger = Logger.getLogger(Synchronizer_Client.class) ;
//	private int interval = 30*1000 ;
	private int batch_size = 100 ;
	private static Synchronizer_Client clientSynchronizer = new Synchronizer_Client() ;
	
	private Synchronizer_Client()
	{
		
	}
	public static Synchronizer_Client getInstance()
	{
		return clientSynchronizer ;
	}
	
	public void run()
	{
        ClientBean client = new ClientBean() ;

        try
        {   
        	int loopTimes = 0 ;
            ConcurrentHashMap<Long,ClientBean>  clientBuffer = Globals.getClientBuffer().getBuffer() ;
            while(keepRunning)
            {
            	loopTimes ++ ;
                Connection conn = null ;
                PreparedStatement ps = null ;
                try
                {
                	conn = ConnectionPool.getConnection() ;
                    ps = client.getPreparedStatement(conn) ;
                    
                    
                    Iterator<ClientBean> clientBeanItr = clientBuffer.values().iterator() ;
                    // 遍历所有活动客户端
                    int i = 0 ;
                    while(clientBeanItr.hasNext())
                    {
                        client = clientBeanItr.next() ;
                        if (client.needSynchronized() || (loopTimes % batch_size == (batch_size-1)))
                        {
                            if(!client.setParameters(ps))
                                continue;
                            ps.addBatch() ;
                            i++ ;
                            if (i%batch_size == (batch_size-1))
                            {
                                ps.executeBatch() ;
                                ps.clearBatch() ;
                            }
                        }
                        
                        if (client.isDeleted())
                        	Globals.getClientBuffer().removeClient(client.getClientId()) ;
                        
                    }
                    
                    ps.executeBatch() ;
                    ps.clearBatch() ;
                    ps.close() ;
                    
                    if (loopTimes % 6 == 0)
                    {
                    	Statement stt = conn.createStatement() ;
                    	stt.executeUpdate(ClientBean.clearSql) ;
                    	stt.close() ;
                    }
                    
                }
                catch (SQLException e)
                {
                    e.printStackTrace() ;
                    logger.error(e.getMessage()) ;
                }
                finally
                {
                	ConnectionPool.freeConn(conn) ;
                }
                // 更新计数比率
                Constants.updateClientPointRatio() ;
                Thread.sleep(interval) ;
            }
        }
        catch (Exception e)
        {
            e.printStackTrace() ;
            logger.error(e.getMessage()) ;
            System.exit(-1) ;
        }	    
	}
	
	
    @Override
    public void init()
    {
        // TODO Auto-generated method stub
        
    }
}
